﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Collections;

//http://xiaobao.nju.edu.cn/news_images/1052/xscan.php?ip=106.187.36.72&aspx=0&title=1&ipver=0&pr=0

namespace SiteScan
{
    public partial class Form1 : Form
    {
        //global value
        string catchFold = "";
        string resultFile = "";
        bool isBing = true;
        bool isRookit = false;
        bool isTools = false;
        bool isForth = false;
        bool isIPFilter = false;

        // 自定义的函数
        #region
        // 定义判断80端口是否开放的函数
        private bool isOpenWeb(IPAddress tIP)
        {
            bool isOpen = false;
            try
            {
                TcpClient tcp = new TcpClient();
                //tcp.SendTimeout = 3;
                //tcp.ReceiveTimeout = 3;
                tcp.Connect(tIP, 80);
                tcp.Close();
            }
            catch (Exception e)
            {
                //处理异常
                isOpen = false;
            }
            return isOpen;
        }

        // 输入IP地址查询旁站信息
        #region
        // 使用bing接口查看旁站
        private ArrayList sideSiteBing(IPAddress tIP)
        {
            ArrayList sideSites = new ArrayList();
            try
            {
                WebClient bingPage = new WebClient();
                //获取或设置用于向Internet资源的请求身份进行验证的网络凭据
                bingPage.Credentials = CredentialCache.DefaultCredentials;
                Byte[] pageData = bingPage.DownloadData("http://cn.bing.com/search?q=ip:173.254.52.43&first=0&count=50");
                //如果获取网站页面采用的是GB2312，则使用这句 
                string pageHtml = Encoding.Default.GetString(pageData);  
                //如果获取网站页面采用UTF8则使用该句
                //string pageHtml = Encoding.UTF8.GetString(pageData);
                MessageBox.Show(pageHtml);
            }
            catch (WebException e)
            {
                MessageBox.Show(e.Message.ToString());
            }
            return sideSites;
        }
        // 使用rootkit接口查看旁站信息
        private ArrayList sideSiteRootkit(IPAddress tIP)
        {
            ArrayList sideSites = new ArrayList();
            //do the things
            return sideSites;
        }
        // 使用tools88接口查看旁站信息
        private ArrayList sideSiteTools(IPAddress tIP)
        {
            ArrayList sideSites = new ArrayList();
            //do the things
            return sideSites;
        }
        #endregion
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        //设置选择自定义IP地址还是导入IP地址的文件
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == false)
            {
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                label1.Enabled = false;
                label2.Enabled = false;
                textBox3.Enabled = true;
                button3.Enabled = true;
                label5.Enabled = true;
            }
            else if(radioButton2.Checked == false)
            {
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                label1.Enabled = true;
                label2.Enabled = true;
                textBox3.Enabled = false;
                button3.Enabled = false;
                label5.Enabled = false;
            }
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm about = new AboutForm();
            about.ShowDialog();
        }

        private void 旁注接口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetForm1 SetWinForm1 = new SetForm1();
            SetWinForm1.ShowDialog();

            isBing = SetWinForm1.getSetForm1Checkbox1.Checked;
            isRookit = SetWinForm1.getSetForm1Checkbox2.Checked;
            isTools = SetWinForm1.getSetForm1Checkbox3.Checked;
            isForth = SetWinForm1.getSetForm1Checkbox4.Checked;
            isIPFilter = SetWinForm1.getSetForm1Checkbox5.Checked;
        }

        private void 缓存文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetForm2 SetWinForm2 = new SetForm2();
            SetWinForm2.ShowDialog();

            catchFold = SetWinForm2.getSetForm2Textbox1.Text;
            resultFile = SetWinForm2.getSetForm2Textbox2.Text;
            MessageBox.Show(catchFold + '\n' + resultFile);
        }

        private void 其它设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetForm3 SetWinForm3 = new SetForm3();
            SetWinForm3.ShowDialog();
        }

        /// <summary>
        /// 导入IP地址文件操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialogFile = new OpenFileDialog();
            dialogFile.InitialDirectory = System.Environment.CurrentDirectory;
            if (dialogFile.ShowDialog() == DialogResult.OK)
            {
                string strResultFile = dialogFile.FileName;
                textBox3.Text = strResultFile;
            }
        }

        //开始自定义的操作
        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress ip;
            if (IPAddress.TryParse(textBox1.Text, out ip))
            {
                sideSiteBing(ip);
            }
            else
            {
                MessageBox.Show("输入的IP地址不合法！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox1.Text = "";
            }
        }

        //  检查IP地址是否有效
        #region
        /// <summary>
        /// 检查IP地址是否有效
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_Leave(object sender, EventArgs e)
        {
            IPAddress ip;
            if (IPAddress.TryParse(textBox1.Text, out ip))
            {

            }
            else
            {
                MessageBox.Show("输入的IP地址不合法！","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                textBox1.Text = "";
            }
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            IPAddress ip;
            if (IPAddress.TryParse(textBox2.Text, out ip))
            {

            }
            else
            {
                MessageBox.Show("输入的IP地址不合法！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox2.Text = "";
            }
        }
        #endregion



    }
}
